﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Forms;
using Microsoft.Xna.Framework.Graphics;
using Blurift.WPFGLControl;
using Patyk.MapEdit;
using Patyk.MapEdit.Helpers;
using Patyk.MapEdit.Infrastructure;
using Application = System.Windows.Application;
using Color = Microsoft.Xna.Framework.Color;

namespace Crevice2D.WPFEditorHost
{
    public partial class BaseWindow : Window
    {
        public Mode Mode { get; set; }
        
        //Editor specifics
        protected Editor _editor;
   
        protected Stopwatch _watch;

        public BaseWindow()
        {
            InitializeComponent();

            _watch = new Stopwatch();

            var graphicsDevice = new GLGraphicsDevice();

            graphicsDevice.LoadContent += LoadContent;
            graphicsDevice.RenderXna += Render;
            graphicsDevice.HwndLButtonDown += HwndLButtonDown;
            graphicsDevice.HwndLButtonUp += HwndLButtonUp;
            graphicsDevice.HwndRButtonDown += HwndRButtonDown;
            graphicsDevice.HwndRButtonUp += HwndRButtonUp;
            graphicsDevice.HwndMouseMove += HwndMouseMove;
            graphicsDevice.SizeChanged += HwndSizeChanged; 
            //graphicsDevice.HwndMouseEnter += HwndMouseEnter;
            //graphicsDevice.HwndMouseLeave += HwndMouseLeave;
            graphicsDevice.Initialize("Content");
            graphicsDevice.AutoScaleMode = AutoScaleMode.None;

            RenderManager.Initialise(graphicsDevice);
            renderWindow.Child = graphicsDevice;

            Mode = Mode.EditNodes;
        }

        private void HwndSizeChanged(object sender, EventArgs e)
        {
            var viewport = new Viewport(0, 0, RenderManager.GLDevice.Width, RenderManager.GLDevice.Height);
            RenderManager.SetViewport(viewport);
        }

        #region HostWindow Event Handlers

        public void LoadContent(object sender, GraphicsDeviceEventArgs e)
        {
            if(!_watch.IsRunning)
            {
                //DPI Awareness
                PresentationSource source = PresentationSource.FromVisual(this);
                if (source != null)
                {
                    float dpiX = (float) source.CompositionTarget.TransformToDevice.M11;
                    float dpiY = (float) source.CompositionTarget.TransformToDevice.M22;
                    PropertiesHelper.SetDPI(dpiX, dpiY);
                }

                RenderManager.LoadContent(e.GraphicsDevice);
               
                _watch.Start();
            }
        }

        public void Render(object sender, GraphicsDeviceEventArgs e)
        {
            RenderManager.Device.Clear(Color.CornflowerBlue);

            RenderManager.SpriteBatch.Begin();

            _editor.Draw();

            RenderManager.SpriteBatch.End();
        }

        private void HwndLButtonDown(object sender, HwndMouseEventArgs e)
        {
            _editor.MouseLDown(e);
        }

        private void HwndLButtonUp(object sender, HwndMouseEventArgs e)
        {
            _editor.MouseLUp(e);
        }

        private void HwndRButtonDown(object sender, HwndMouseEventArgs e)
        {
            _editor.MouseRDown(e);
        }

        private void HwndRButtonUp(object sender, HwndMouseEventArgs e)
        {
            _editor.MouseRUp(e);
        }

        private void HwndMouseMove(object sender, HwndMouseEventArgs e)
        {
            _editor.UpdateMouse(e);
            workSpaceWidth.Text = string.Format("Width: {0}", ((int) e.Position.X).ToString());
            workSpaceHeight.Text = string.Format("Height: {0}", ((int) e.Position.Y).ToString());
        }

        //private void HwndMouseEnter(object sender, HwndMouseEventArgs e)
        //{
        //    Mouse.OverrideCursor = Cursors.Cross;
        //}

        //private void HwndMouseLeave(object sender, HwndMouseEventArgs e)
        //{
        //    Mouse.OverrideCursor = Cursors.Arrow;
        //}

        #endregion

        #region WPF Window Event Handlers

        private void newMapMnu_Click(object sender, RoutedEventArgs e)
        {
            var newMapModal = new NewMapModal();
            var mapData = newMapModal.ShowNewMapDialog();
            if(mapData != null)
            {
                //var map = new Map(mapData.Name, mapData.Width, mapData.Height);
            }
        }

        private void exitMnu_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        #endregion
    }
}
